# Kestrel: An XMPP-based Many-Task Computing Scheduler
# Author: Lance Stout <lancestout@gmail.com>
#
# Copyright 2009-2010 Clemson University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
LifeCyle Module

The life cycle module is responsible for applying and removing
application roles. As such, it is the first module that should
be registered with the application kernel. Once the kernel starts,
the life cycle module can either start specific roles, or attempt
to automatically discover what roles should be applied.

WARNING: This module is currently experimental and will likely
undergo major revision. A method for role discovery has not yet
been chosen.
"""

class ManagerLifeCycle(object):
    """
    The life cycle module is responsible for applying and removing
    application roles. As such, it is the first module that should
    be registered with the application kernel. Once the kernel starts,
    the life cycle module can either start specific roles, or attempt
    to automatically discover what roles should be applied.

    WARNING: This module is currently experimental and will likely
    undergo major revision. A method for role discovery has not yet
    been chosen.

    Events listened for by this module:
      kernel/start
      kernel/stop
      role/begin
      role/end
      role/discover
      system/start
      system/shutdown

    Events raised by this module:
      role/begin
      role/discover
    """
    def __init__(self, kernel):
        self.kernel = kernel
        self.events = {'kernel/start': self.kernel_start,
                       'kernel/stop': self.kernel_stop,
                       'role/begin': self.start_role,
                       'role/end': self.end_role,
                       'role/discover': self.discover_role,
                       'system/online': self.system_online,
                       'system/shutdown': self.system_shutdown}
        self.kernel.register(self)


    def start_role(self, name):
        role = self.kernel.config('kernel/roles/'+name)
        if role is not None:
            role.register(self.kernel)


    def end_role(self, name):
        pass


    def discover_role(self, data=None):
        self.kernel.event('role/begin', 'system')


    def kernel_start(self, data=None):
        self.kernel.event('role/discover')


    def kernel_stop(self, data=None):
        pass


    def system_online(self, data=None):
        self.kernel.event('role/begin', 'manager')


    def system_shutdown(self, data=None):
        pass
